package com.metaera.app.utils;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 页面导航
 */
public class ActivityNavigateManager implements Application.ActivityLifecycleCallbacks {

    private static ActivityNavigateManager sActivityManager;


    public static ActivityNavigateManager instance() {
        if (sActivityManager == null) {

            synchronized (ActivityNavigateManager.class) {

                if (sActivityManager == null) {
                    sActivityManager = new ActivityNavigateManager();
                }

            }
        }

        return sActivityManager;
    }


    /**
     * 跳转指定页面，关闭其他所有页面
     */
    public static void navigate(Context context, Class<?> clz) {
        context.startActivity(new Intent(context, clz));

        for (Activity activity : sActivityManager.mActivityList) {
            if (activity.getClass() != clz) {
                activity.finish();
            }
        }
    }

    public static void finish(Class<?>... clzList) {
        for (Activity activity : sActivityManager.mActivityList) {
            for (Class<?> clz : clzList) {
                if (activity.getClass() == clz) {
                    activity.finish();
                }
            }
        }
    }

    public static void range(Context context, Class<?> clz) {
        List<Activity> targetActivities = new ArrayList<>();
        for (Activity activity : sActivityManager.mActivityList) {
            if (!targetActivities.isEmpty()) {
                targetActivities.add(activity);
            }

            if (activity.getClass() == clz && targetActivities.isEmpty()) {
                targetActivities.add(activity);
            }
        }

        for (Activity activity : targetActivities) {
            activity.finish();
        }

        context.startActivity(new Intent(context, clz));
        Log.i("ActivityNavigateManager", targetActivities.toString());
    }


    List<Activity> mActivityList = new ArrayList<>();

    @Override
    public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {
        if (mActivityList.isEmpty()) {
            PushAgent.getInstance(activity).onAppStart();
        }

        mActivityList.add(activity);
    }

    @Override
    public void onActivityStarted(@NonNull Activity activity) {

    }

    @Override
    public void onActivityResumed(@NonNull Activity activity) {
        //MobclickAgent.onResume(activity);
        MobclickAgent.onPageStart(activity.getClass().getName());
    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {
//        MobclickAgent.onPause(activity);
        MobclickAgent.onPageEnd(activity.getClass().getName());
    }

    @Override
    public void onActivityStopped(@NonNull Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {

    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {
        mActivityList.remove(activity);
    }

}
